from sqlalchemy import create_engine
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import sessionmaker, scoped_session
from contextlib import contextmanager

engine = create_engine(
    "mysql+pymysql://root:root@localhost/reptile_test?charset=utf8",
    echo=False,
    pool_size=100
)

Base = declarative_base()


@contextmanager
def auto_session_maker(session=scoped_session(sessionmaker(bind=engine))):
    try:
        yield session
        session.commit()
    except BaseException as e:
        print('Session Maker Error: ', e)
        session.rollback()
    finally:
        session.close()
